
"""
copyright (c) 2009 Claudio Canepa
this code licensed under the new BSD license, see file license_simpledocu.txt
"""
import sys, os
import re

from iwrap import iwrap as wrap

    
def text_from_file(fname):
    f = open(fname,'rb')
    text = f.read()
    f.close()
    text.replace('\r','')
    return text

def text_to_file(text, fname):
    f = open(fname,'wb')
    f.write(text)
    f.close()

from inoutMockup import InMockup, OutMockup
# copiada de test_iwrap.g03_iwrap_simpletags y adaptada
def helper_get_iwrapped_text(text_in, tabsize, maxwidth, simpletags):
    inm = InMockup(text_in)
    it = inm.get_line_iterator()
    out = OutMockup()
    wrap(it, out.write, tabsize, maxwidth, simpletags)
    text_out = out.text
    return text_out

dest_dir = 'aia_changed'
tabsize = 4
maxwidth = 75
simpletags = {'+':1,'w':1,'?':1}

# limpiar el directorio -> se hace desde el bat
os.mkdir(dest_dir)
# leer aia_status.txt , parsear la lista de cambiados. (M , A, quizas ?)
f = open('aia_status.txt','rb')
text = f.read()
f.close()
text = text.replace('\r','')
lines = text.split('\n')
for line in lines:
    line = line.rstrip('\n')
    if not line:
        continue
    line = line[1:]
    line = line.strip()
    print line.__repr__()

    fname = line
    text_in = text_from_file(fname)
    text_out = helper_get_iwrapped_text(text_in, tabsize, maxwidth, simpletags)
    fnameout = os.path.join(dest_dir,os.path.basename(fname))
    text_to_file( text_out, fnameout)

# ok, el primer file convirtio bien.
# estaria la opcion de reemplazar las cadenas @...@ para ya insertar los links.


    

